package com.razorfish.configuration.entities 
{
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author derek
	 */
	public class SingleConfigObject extends ConfigObject
	{
		public var parent:Group;
		public var uniqueName:String;
		public var simpleName:String;
		public var isValueReady:Boolean;
		protected var _value:*;
		
		private static var _propDict:Dictionary;
		private static var _itemDict:Dictionary;
		
		public function SingleConfigObject(data:*, parent:Group)
		{
			super(data);
			this.parent = parent;
		}
		
		public function get hasDirectValue():Boolean
		{
			if (this is PredefinedProperty) return true;
			else if (this.dependsUpon == null) return true;
			else return false;
		}
		
		public function get value():*
		{
			return _value;
		}
		
		protected static function registerItem(item:Item):void
		{
			register(item);
		}
		
		protected static function registerProperty(prop:Property):void
		{
			register(prop);
		}
		
		public function tryGetValue():void
		{
			
		}
		
		private static function register(obj:SingleConfigObject):void
		{
			var dict:Dictionary;
			if (obj is Property)
			{
				if (_propDict == null)
					_propDict = new Dictionary();
				dict = _propDict;
			}
			else if (obj is Item)
			{
				if (_itemDict == null)
					_itemDict = new Dictionary();
				dict = _itemDict;
			}
			
			if (dict[obj.simpleName] == undefined)
			{
				dict[obj.simpleName] = [obj];
			}
			else
			{
				(dict[obj.simpleName] as Array).push(obj);
			}
		}
		
		public static function getProperty(key:String):*
		{
			if (PredefinedProperty.isDefined(key)) return PredefinedProperty.getProperty(key);
			
			if (_propDict == null) return null;
			if (_propDict[key] == undefined) return null;
			else return _propDict[key];
		}
		
		public static function getItem(key:String):Array
		{
			if (_itemDict == null) return null;
			if (_itemDict[key] == undefined) return null;
			else return _itemDict[key];
		}
		
		public static function getSingleConfigObject(pair:KeyTypePair):Array
		{
			if (pair.type == ConfigObjectType.Property) return getProperty(pair.key);
			else if (pair.type == ConfigObjectType.Item) return getItem(pair.key);
			else return null;
		}
		
		public static function dump():void
		{
			trace("--- dump start ---");
			trace("--- props ---");
			for (var propKey:String in _propDict)
			{
				var propArr:Array = _propDict[propKey] as Array;
				trace(propKey, propArr.length, propArr[0].value);
			}
			trace("--- items ---");
			for (var itemKey:String in _itemDict)
			{
				var itemArr:Array = _itemDict[itemKey] as Array;
				trace(itemKey, itemArr.length, itemArr[0].value);
			}
			trace("--- dump end ---");
		}
	}
}
